/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package is202.hrms.web;

import is202.hrms.ejb.FileEJB;
import is202.hrms.ejb.ModulenEJB;
import is202.hrms.ejb.ProgressEJB;
import is202.hrms.ejb.UserEJB;
import is202.hrms.entity.Progid;
import is202.hrms.entity.Progress;
import is202.hrms.entity.StoredFile;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import org.apache.myfaces.custom.fileupload.UploadedFile;

/**
 * This class demonstrates how to handle file up and downloads in backing beans.
 *
 * @author even
 */
@Named("fileBean")
@RequestScoped
public class UploadDemoBean {

    @EJB
    FileEJB fileEjb;
    @EJB
    ProgressEJB progressEjb;
    @EJB
    ModulenEJB modEjb;
    @EJB
    UserEJB userEjb;
    private UploadedFile file;
    long id;

    public UploadDemoBean() {
    }

    /**
     * This method will be called when the user clicks the submit button in the
     * file upload form.
     */
    public String submit() throws IOException {
        if (null == file) {
            return "start-modul.xhtml";
        }

        StoredFile dbFile = new StoredFile(file);
        StoredFile dbFile2 = fileEjb.insert(dbFile);
        id = dbFile2.getId();
        return "start-modul.xhtml?id=0";
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public StoredFile submit2() throws IOException {
        if (null == file) {

        }

        StoredFile dbFile = new StoredFile(file);
        StoredFile dbFile2 = fileEjb.insert(dbFile);
        id = dbFile2.getId();
        return dbFile2;
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile f) {
        System.out.println("Upload: Receieved fiel " + f.getName() + " " + f.getContentType() + " " + f.getSize());
        this.file = f;
    }

    public List<StoredFile> getFiles() {
        return fileEjb.findAll();
    }

    public String progressFeedback(int stud, int mod) throws IOException {

      
        Progress piss = new Progress();
        piss.setModuler(modEjb.find(mod));
        piss.setBruker(userEjb.find(stud));

        StoredFile dbFile = submit2();

        piss.setStoredfile(dbFile);
        progressEjb.insert(piss);
        return "start-modul.xhtml";
    }

}
